Examples
Solve these matrix multiplication problems in Matlab
To solve a matrix multiplication problem like this, enter each matrix and assign it to a name. Then use the * operator and assign the results to a name.
A=[1 3 4; 3 2 3; 5 1 2]
B=[2 3 1; 4 1 1; 2 3 4]
C=A*B
The result is
Since the first matrix was already entered and saved as A in the first problem, you only need to enter the second matrix. If you have used "clear", you would have to enter both matrices.
V=[3;4;2]
W=A*V
This code leads to the following output.W = 22 23 23
Since the second matrix is the identity matrix, we can use Matlab's identity matrix function,
eye
.F=eye(3)
T=A*F
This code leads to the following output.
Use Matlab to compute the differences between each value and the next in this sequence of values, 22, -14.6, 23.2, 18.4, 56.3?
Tip: You can enter the values in a single matrix and then create two parts so that one part can be subtracted from the other. In that way, each value is only entered once and there is less chance for error.
Note: To access only some rows (and columns) of a saved matrix, use this syntax:
matrixName( vectorOfRowNumbers, vectorOfColNumbers )
. If you want all rows (or all columns) use a:
. For example,matrixName( rowNum, : )
will return all columns of rowrowNum
.If you want your code to work for a matrix of any length, use the
length
function to get the number of elements in the matrix.If you need to know the number of rows and columns that a matrix contains, use the
size
function and save the results to two variables, like this.>> [ numRows, numCols ] = size( myMatrixName )
V = [22, -14.6, 23.2, 18.4, 56.3]
v1 = V(1:4) % get all but the last value
v2 = V(2:5) % get all but the first value
A = v2-v1P = [ ... ]
n = length(P)
p1 = P(1:n-1)
p2 = P(2:n)
B = p2-p1This technique is very powerful. Simply create a new matrix that contains the subset of the original matrix to perform the type of calculation that you need.
Use Matlab to compute the differences between each value and the next in its same column of values.
Note: To access only some rows (and columns) of a saved matrix, use this syntax:
matrixName( vectorOfRowNumbers, vectorOfColNumbers )
Tip: You can still the same trick from the previous example with two new matrices.
P = [89.7,24,9.9; 88.5,23.5,10; 78.6,17,10; 77.5,22,7.8; 69.9,23.4,6.4; 67.3,21.8,8.75]
n = length(P)
p1 = P(1:n-1 , :) % get all but last row
p2 = P(2:n , :) % get all but first row
B = p2-p1Write Matlab code to create this table and name it SCRIBNER_DECIMAL_C
If it helps in some way to create rows or columns, use Matlab's colon operator with a specific increment amount.
For example:
10:10:80
creates a sequence of the values 10, 20, 30, 40, 50, 60, 70, 80.Unfortunately, there was very little to be gained by using the colon operator. But, I did find a few spots where it saved a few key strokes.
scribner_decimal_c = [6 5 5 10 10 10 20 ; ...
7 5 10 10 20 20 30 ; ...
8 10 10 20 20 20 30 ; ...
9 10 20 30 30 30 40 ; ...
10 20 30 30 30 40 60 ; ...
11 20:10:40 40 50 70 ; ...
12 30:10:80 ; ...
13 40:80 100 ; ...
14 40 60 70 90 100 110; ...
15 50:20:110 120 140; ...
16 60:20160 ; ...
17 70 90 120:20:180; ...
18 80 110 130:30;210; ...
19 90:30:240; ...
20 110 140 170 210 240 280; ...
21 120 150 190 230 270 300; ...
22 130 170 210 250 290 330; ...
23 140 190 230 280 330 380; ...
24 150 210 250 300 350 400; ...
25 170 230 290 340 400 460; ...
26 190 250 310 370 440 500; ...
27 210 270 340 410 480 550; ...
28 220 290 360 440 510 580; ...
29 230 310 380 460 530 610; ...
30 250 330 410 490 570 660]